Skip to main content

API Design

35. What are the principles of good API design?

ভালো API design করলে developer experience ভালো হয়, integration সহজ হয় এবং সিস্টেম maintain করা সহজ হয়।

What makes an API RESTful?

REST (Representational State Transfer) এর মূল নীতিগুলো:

  1. Stateless: প্রতিটি request স্বয়ংসম্পূর্ণ — server client এর আগের state মনে রাখে না।
  2. Uniform Interface: HTTP methods সঠিকভাবে ব্যবহার করুন।
  3. Resource-based: URL এ noun ব্যবহার করুন, verb নয়। ✅ /users/123/getUser?id=123
  4. Stateless: Server client state store করে না।
  5. Cacheable: Response cacheable হওয়া উচিত।

What is the Richardson Maturity Model?

Levelনামবৈশিষ্ট্য
0The Swamp of POXএকটিই endpoint, সব POST — SOAP-like
1Resourcesআলাদা resource URL: /users, /orders
2HTTP VerbsGET, POST, PUT, DELETE সঠিকভাবে
3Hypermedia (HATEOAS)Response এ next possible actions এর link থাকে

How do you version a public API?

  • URL Path (সবচেয়ে সাধারণ): /api/v1/users, /api/v2/users
  • Header: Accept: application/vnd.myapp.v2+json
  • Query Param: /users?version=2 — সহজ কিন্তু ugly।
  • Best practice: URL path versioning করুন, Semantic versioning follow করুন।

36. What is the difference between REST, GraphQL, and gRPC?

বৈশিষ্ট্যRESTGraphQLgRPC
ProtocolHTTP/1.1HTTPHTTP/2
FormatJSON/XMLJSONProtocol Buffers (binary)
TypingManual/OpenAPIStrongly typed schemaStrongly typed .proto
Over/Under-fetchingসমস্যা আছেনেই — ঠিক যা চাই তাইনেই
Browser Support✅ সহজ⚠️ gRPC-Web দরকার
Best ForPublic APIsFlexible client needsMicroservices, low latency

When would you choose GraphQL over REST?

  • বেছে নিন GraphQL যখন: Client বিভিন্ন ধরনের data combination দরকার, mobile app bandwidth সাশ্রয় করতে চায়, rapid front-end iteration।
  • এড়িয়ে চলুন: Simple CRUD, caching জটিল (query unique বলে HTTP cache কঠিন), team GraphQL জানে না।

What are the performance benefits of gRPC over REST?

  • Protocol Buffers: JSON এর চেয়ে ৫-১০x ছোট payload।
  • HTTP/2: Multiplexing — একটি connection এ একসাথে অনেক request।
  • Streaming: Server-side, client-side, bidirectional streaming support।
  • Code generation: .proto file থেকে আপনাআপনি client/server code generate।

What is the n+1 query problem in GraphQL and how is it solved?

Query: { users { posts { comments } } }
N+1: 1 (users fetch) + N (posts for each user) + N*M (comments for each post)
  • সমাধান: DataLoader — request গুলো batch করে একটি query তে পাঠায়।

37. How do you design a pagination API for large datasets?

What is the difference between offset pagination and cursor-based pagination?

বৈশিষ্ট্যOffset PaginationCursor-based Pagination
URL/posts?page=2&limit=20/posts?cursor=xyz&limit=20
DB QueryLIMIT 20 OFFSET 40WHERE id > last_id LIMIT 20
Performanceবড় offset এ slow (DB পুরোটা scan করে)সবসময় fast
Real-time dataনতুন item যোগ হলে page shift হয়Stable — cursor based
Random accessযেকোনো page এ যাওয়া যায়শুধু next/prev

Why is cursor-based pagination preferred for real-time feeds?

  • Twitter/Instagram feed এ নতুন post আসতে থাকে।
  • Offset pagination এ page 2 দেখলে, নতুন 5টি post আসলে পুরনো items shift হয়ে duplicate বা missing দেখায়।
  • Cursor (last seen item ID) stable reference — যতই নতুন item আসুক, cursor থেকে continue হয়।

How do you handle page size limits in a public API?

  • Default limit সেট করুন: limit=20
  • Maximum limit enforce করুন: max limit=100 — ইউজার বেশি চাইলেও ১০০ দিন।
  • Response এ next_cursor, has_more, total_count দিন।

38. What is rate limiting in APIs and how do you implement it?

Rate Limiting হলো প্রতি ইউজার/IP কে নির্দিষ্ট সময়ে নির্দিষ্ট সংখ্যক request এর অনুমতি দেওয়া।

What algorithms are used for rate limiting?

Algorithmকীভাবে কাজ করেPros/Cons
Token Bucketপ্রতি second একটি bucket এ token যোগ হয়, request এলে token খরচBurst allow করে
Leaky BucketQueue এ request রাখে, fixed rate এ process করেSmooth output
Fixed Window Counterপ্রতি minute window তে count রাখেWindow edge এ spike সম্ভব
Sliding Window LogLast N seconds এর সব request timestamp track করেMost accurate, memory বেশি

How do you implement distributed rate limiting?

  • Redis + Lua Script: Atomic operation দিয়ে counter increment।
  • Redis এ key: rate_limit:{user_id}:{window} — INCR + EXPIRE।
MULTI
INCR user:123:1702000000
EXPIRE user:123:1702000000 60
EXEC

Count > 100 → reject with 429

How do you communicate rate limit status to clients (HTTP headers)?

HTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 43
X-RateLimit-Reset: 1702000060
Retry-After: 30

39. How do you design an idempotent API?

Idempotent API: একই request একাধিকবার পাঠালেও ফলাফল একই — no unwanted side effects।

Which HTTP methods are idempotent by definition?

MethodIdempotent?Safe?ব্যাখ্যা
GETশুধু পড়ে, পরিবর্তন করে না
PUTReplace করে — বারবার করলে same result
DELETE২য় DELETE = 404, কিন্তু state same
POSTবারবার করলে duplicate তৈরি
PATCH❌ (usually)Depends on implementation

How do you make a POST endpoint idempotent using idempotency keys?

  • Client একটি unique Idempotency-Key header পাঠায়।
  • Server সেই key দেখে: আগে process হয়েছে কি? হলে same response return করো।
POST /payments
Idempotency-Key: abc-123-xyz

{ "amount": 1000, "to": "user_456" }

40. How do you handle API versioning?

What are the different API versioning strategies?

কৌশলউদাহরণProsCons
URL Path/api/v1/usersস্পষ্ট, cacheableURL ugly হয়
Query Param/users?v=1EasyNon-standard
Request HeaderAPI-Version: 2024-01Clean URLকম visible
Accept HeaderAccept: application/vnd.v2+jsonHTTP standardComplex

How long should you maintain backward compatibility in a versioned API?

  • ন্যূনতম ৬-১২ মাস deprecation notice দেওয়ার পরে।
  • Sunset header দিন: Sunset: Sat, 31 Dec 2024 00:00:00 GMT
  • Email notification + documentation update।
  • Breaking change এ major version bump করুন (v1 → v2)।

41. What is an API gateway and what does it provide?

API Gateway হলো সব external client request এর single entry point।

Mobile App  ─┐
Web App ─┤→ API Gateway → User Service
3rd Party ─┘ → Order Service
→ Product Service

What is the difference between an API gateway and a reverse proxy?

বৈশিষ্ট্যReverse Proxy (Nginx)API Gateway (Kong, AWS API GW)
LayerNetwork/HTTP levelApplication level
ক্ষমতাLoad balance, SSL termination, cacheAuth, rate limit, transform, routing, analytics
Business Logicনেইসীমিত থেকে বেশি
উদাহরণNginx, HAProxyAWS API Gateway, Kong, Envoy

How does an API gateway handle authentication and authorization?

  • Authentication: Token validate করা (JWT, OAuth) — backend কে একাধিকবার করতে হয় না।
  • Authorization: Policy based — "এই API endpoint এ শুধু admin যাবে।"
  • API Key management: Third-party developer দের জন্য।

What is a BFF (Backend for Frontend) pattern?

  • প্রতিটি frontend (mobile, web, TV) এর জন্য আলাদা gateway যা তাদের specific needs অনুযায়ী response shape করে।
  • Mobile কে compact response, web কে full response।
  • GraphQL প্রায়ই BFF pattern implement করে।